package game.block.trap;

import game.block.Locator;
import game.block.command.Command;
import game.block.command.RemoveCommand;
import game.block.entity.MovableEntity;
import game.block.entity.TrapEntity;
import game.block.game.Board;
import game.block.tile.Tile;
import game.block.turn.Turn;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.Vector2;

public class Spike extends TrapEntity {

    private int state;
    private Sprite spikeUpSprite;
    private Sprite spikeDownSprite;

    public Spike(Vector2 pos, Tile tile, int state) {
        super(pos, tile, null, TRAPTYPE.SPIKE);
        this.state = state;
        spikeUpSprite=new Sprite(new Texture(Gdx.files.internal(SPIKE_UP)));
        spikeDownSprite=new Sprite(new Texture(Gdx.files.internal(SPIKE_DOWN)));
        switchState();
    }

    @Override
    public void update(float delta) {
        super.update(delta);
    }

    public void switchState() {
        if (state == 1) {
            sprite = spikeUpSprite;
            state = 2;
        } else {
            sprite = spikeDownSprite;
            state = 1;
        }
    }

    @Override
    public Command advance() {
        switchState();
        return null;
    }

    @Override
    public void applyEffect(MovableEntity block, Board gameBoard, Turn currentTurn, Tile tileMovedTo) {
        if (state == 1) {
            if (!block.counterEffect(this)) {
            	Locator.getAudio().playSound(SOUND.SPIKE.getValue());
                currentTurn.addCommand(new RemoveCommand(block, currentTile, gameBoard));
            }
        }
    }

    @Override
    public Command retract() {
        switchState();
        return null;
    }
    
    @Override
	public void setInfo() {
		info="Name: Spike Trap\nFunction: Destroys a block when the spikes raise";
	}
}
